package student.aistar.watchtask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import student.aistar.service.IWatchSeckillService;

import java.util.List;
import java.util.Map;

@Component
public class TaskWatchSeckill {

    @Autowired
    private IWatchSeckillService iWatchSeckillService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 每隔两秒执行一次
    @Scheduled(cron = "0/2 * * * * *")
    public void testTask01(){

        List<Map<String,Object>> datas = iWatchSeckillService.beforeActive();

        if (datas!=null && datas.size()>0){
//            System.out.println("sorry! 秒杀活动尚未开始!");

            ListOperations lop = redisTemplate.opsForList();

            ValueOperations vop = redisTemplate.opsForValue();

            // 遍历datas
            for (Map<String, Object> data : datas) {
                long id = (long) data.get("id");
                Integer num = (Integer) data.get("num");
                //获取list集合为sk_id的大小
                long size = lop.size("sk_"+id);
//                System.out.println("进来了");
                if(size==0) {
                    //存储当前秒杀id的状态
                    vop.set("st_"+id,"0");
                    for (Integer i = 0; i < num; i++) {
                        //保存秒杀id
                        lop.leftPush("sk_" + id, id);
                    }
                }
            }
        }else{
//            System.out.println("加载失败!");
        }
    }

    /**
     * 任务二 正在进行秒杀,尚未结束
     * 更新redis中的status 0 -> 1
     * 更新mysql中的status 0 -> 1
     */
    @Scheduled(cron = "0/2 * * * * *")
    public void task02(){
        ValueOperations vop = redisTemplate.opsForValue();
        List<Long> ids = iWatchSeckillService.selectKillId();
        if (ids != null && ids.size()>0){
//            System.out.println("秒杀活动已经开始!!!");
            // 进行状态更新
            for (Long id : ids) {
                //更新redis
                vop.set("st_"+id,"1");
            }
        }
        // 更新mysql
        iWatchSeckillService.updateStatus();
    }

    /**
     * 任务三 秒杀活动已经结束
     * 更新mysql中的status 1 -> 2
     * 删除redis中的数据
     */
    @Scheduled(cron = "0/2 * * * * *")
    public void task03(){
        List<Long> ids = iWatchSeckillService.selectEndKillid();
        if (ids != null && ids.size()>0){
//            System.out.println("sorry,秒杀活动已经结束了,您来晚一步");
            // 进行状态更新
            for (Long id : ids) {
                //直接删除redis
                redisTemplate.delete("sk_"+id);
                redisTemplate.delete("st_"+id);
            }
        }
        // 更新mysql
        iWatchSeckillService.updateEndStatus();
    }
}
